/**
 * 
 */
package list.options;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

/**
 * @author JJ
 *
 */
public class ShowMapsActivity extends Activity {
	
	/* This html page should be uploaded to some place where we can all use it. */
	private final String MAPURL = "http://www.jeroenzijp.nl/apps_nh/view.html";
	
	private WebView map_viewer;
	
	/* Some static coordinates for testing purposes */
	private double[] latitudes = {};
	private double[] longitudes = {}; 
	
	/* Some static string with content */
	private final String content = "hiephoi!";
	
	private ArrayList ParentName, ObjectName; 
		
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.map);
		
		/* Get the arraylist from the bundle */
		try {
			Bundle bundle = getIntent().getExtras();
			latitudes = getLatitudes(bundle.getStringArrayList("Coordinates"));
			longitudes = getLongitudes(bundle.getStringArrayList("Coordinates"));
			ParentName = bundle.getStringArrayList("ParentName");
			ObjectName = bundle.getStringArrayList("Name");
		}
		catch(Exception ex) { Log.e("ERROR", ex.toString()); }
		
		/* Show the map in the WebView control. */
		try {
			map_viewer = (WebView) findViewById(R.id.webview);
			map_viewer.getSettings().setJavaScriptEnabled(true);
			map_viewer.setWebViewClient(new WebViewClient() {
				@Override
				public void onPageFinished(WebView view, String url) {
					for (int i = 0; i < latitudes.length; i++) {
						map_viewer.loadUrl(getMarkerURL(latitudes[i], longitudes[i], ParentName.get(i).toString(), ObjectName.get(i).toString()));
					}
				}
			});
			map_viewer.loadUrl(MAPURL);
		}
		catch(Exception ex) {
			Toast.makeText(this, ex.toString(), Toast.LENGTH_LONG).show();
		}
	}
	
	private String getMarkerURL(double lat, double lng, String img, String title) {
		//Toast.makeText(this, "javascript:addMarker(" + lat + ", " + lng + ", " + img + ", " + title + ")", Toast.LENGTH_LONG).show();
		return "javascript:addMarker(" + lat + ", " + lng + ", '" + img + "', '" + title + "')";
	}
	
	private class JavaScriptInterface {
		public String getPopupContent() {
			return content;
		}
	}
	
	/**
	 * Function to get latitudes from the array list.
	 * @param array containing location data.
	 * @return double array containing only latitudes.
	 */
	private double[] getLatitudes(ArrayList<String> array) {
		double[] lats = new double[array.size()];
		String[] coords = {};
		
		for(int i = 0; i < array.size(); i++) {
			coords = ((String)array.get(i)).split("\\|");
			//Log.w("latitudes", coords[0] + " " + coords[1]);
			lats[i] = (Double)((Double.parseDouble(coords[0]) + Double.parseDouble(coords[1])) / 2);
		
			//Log.w("Lat", String.valueOf(lats[i]));
		}	
		
		return lats;
	}
	
	/**
	 * Function to get longitudes from the array list.
	 * @param array containing location data.
	 * @return double array containing only longitudes.
	 */
	private double[] getLongitudes(ArrayList<String> array) {
		double[] lngs = new double[array.size()];
		String[] coords = {};
		
		for(int i = 0; i < array.size(); i++) {
			coords = ((String)array.get(i)).split("\\|");
			//Log.w("longitudes", coords[2] + " " + coords[3]);
			lngs[i] = (Double)((Double.parseDouble(coords[2]) + Double.parseDouble(coords[3])) / 2);
			
			//Log.w("Lng", String.valueOf(lngs[i]));
		}
			
		return lngs;
	}
	
	
	
	
	
	
	
	
}
